package com.example.cushion.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ViewGroup;

import com.example.cushion.R;
import com.example.cushion.ui.loading.ILoading;
import com.example.cushion.ui.loading.LoadingView;

import org.itzheng.and.activity.ItActivity;
import org.itzheng.and.baseutils.ui.UIUtils;

import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2018-3-2.
 */
public class BaseActivity extends ItActivity implements BGASwipeBackHelper.Delegate, ILoading {
    private static final String TAG = "BaseActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initSwipeBackFinish();
        super.onCreate(savedInstanceState);
        setHideActionBar(true);
    }

    public Activity getActivity() {
        return this;
    }

    protected BGASwipeBackHelper mSwipeBackHelper;

    @Override
    public void onBackPressed() {
        Log.d(TAG, "onBackPressed " + 1);
        if (mSwipeBackHelper.isSliding()) {
            //正在滑动的时候取消返回事件
            return;
        }
        if (isLoading()) {
            if (true) {
                //如果正在加载，并且可以取消则取消
                dismissLoading();
            }
            return;
        }
        //先判断popup，因为pop是最外面显示的
//        if (basePopups != null && !basePopups.isEmpty()) {
//            for (BasePopupWindow popupWindow : basePopups) {
//                if (popupWindow.onBackPressed()) {
//                    return;
//                }
//            }
//        }
//        if (baseFragments != null && !baseFragments.isEmpty()) {
//            for (BaseFragment fragment : baseFragments) {
//                if (fragment.onBackPressed()) {
//                    return;
//                }
//            }
//        }
        if (isSupportSwipeBack()) {
            //如果支持滑动关闭则使用插件回调
            mSwipeBackHelper.backward();
        } else {
            //不支持滑动关闭使用系统返回
            super.onBackPressed();
        }

    }

    /**
     * 初始化滑动返回。在 super.onCreate(savedInstanceState) 之前调用该方法
     */
    private void initSwipeBackFinish() {
        mSwipeBackHelper = new BGASwipeBackHelper(this, this);
        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
        // 下面几项可以不配置，这里只是为了讲述接口用法。

        // 设置滑动返回是否可用。默认值为 true
        mSwipeBackHelper.setSwipeBackEnable(true);
        // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
        mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true);
        // 设置是否是微信滑动返回样式。默认值为 true
        mSwipeBackHelper.setIsWeChatStyle(true);
        // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
        mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow);
        // 设置是否显示滑动返回的阴影效果。默认值为 true
        mSwipeBackHelper.setIsNeedShowShadow(true);
        // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
        mSwipeBackHelper.setIsShadowAlphaGradient(true);
        // 设置触发释放后自动滑动返回的阈值，默认值为 0.3f
        mSwipeBackHelper.setSwipeBackThreshold(0.3f);
        // 设置底部导航条是否悬浮在内容上，默认值为 false
        mSwipeBackHelper.setIsNavigationBarOverlap(true);
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public void onSwipeBackLayoutSlide(float slideOffset) {

    }

    @Override
    public void onSwipeBackLayoutCancel() {

    }

    /**
     * 滑动返回执行完毕，销毁当前 Activity
     */
    @Override
    public void onSwipeBackLayoutExecuted() {
        mSwipeBackHelper.swipeBackward();
    }

    private LoadingView mLoadingView;

    @Override
    public void showLoading() {
        if (mLoadingView == null) {
            mLoadingView = new LoadingView(UIUtils.getContext());
            ViewGroup viewParent = (ViewGroup) getWindow().getDecorView();
            mLoadingView.attachToRoot(viewParent);
        }
        mLoadingView.showLoading();
    }

    @Override
    public void dismissLoading() {
        if (mLoadingView == null) return;
        mLoadingView.dismissLoading();
    }

    @Override
    public boolean isLoading() {
        return mLoadingView != null && mLoadingView.isLoading();
    }
}
